home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / snpd0493.zip / MTERM.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  4KB  |  134 lines

  1. /* MTERM.C - Minimal example PC terminal program.
  2.    Released to public domain by author, David Harmon, June 1992.
  3.    Intended for use with a FOSSIL driver, but will run with BIOS alone.
  4.    I expect you'll want to add something for practical purposes. ;-)
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <conio.h>            /* kbhit(), getch(), putch(), etc. */
  10. #include <dos.h>              /* int86(), etc. */
  11.  
  12. #ifdef __ZTC__
  13.  #define cputs(s) fputs((s),stderr)
  14. #endif
  15.  
  16. int port = 0;                 /* 0 = COM1:, 1 = COM2: etc. */
  17. int local_echo = 0;
  18. int cr_add_lf = 0;
  19. int exiting = 0;
  20.  
  21. int init_comm(int flags)
  22. {
  23.       union REGS regs;
  24.  
  25.       regs.h.ah = 0x04;         /* initialize driver (port) */
  26.       regs.x.bx = 0x4f50;
  27.       regs.x.dx = port;
  28.       int86( 0x14, ®s, ®s);
  29.  
  30.       regs.h.ah = 0x00;         /* set baud rate * port attrs */
  31.       regs.h.al = (unsigned char)flags;
  32.       regs.x.dx = port;
  33.       int86( 0x14, ®s, ®s);
  34.       return regs.h.ah;
  35. }
  36.  
  37. void send_char(char ch)
  38. {
  39.       union REGS regs;
  40.  
  41.       regs.h.ah = 0x01;         /* Send char (wait until ready)*/
  42.       regs.h.al = ch;
  43.       regs.x.dx = port;
  44.       int86( 0x14, ®s, ®s);
  45. }
  46.  
  47. int   input_ready(void)
  48. {
  49.       union REGS regs;
  50.  
  51.       regs.h.ah = 0x03;         /* Get port status */
  52.       regs.x.dx = port;
  53.       int86( 0x14, ®s, ®s);
  54.       return ((regs.h.ah & 0x01) != 0);   /* input ready */
  55. }
  56.  
  57. int   get_char(void)
  58. {
  59.       union REGS regs;
  60.  
  61.       regs.h.ah = 0x02;         /* receive char (wait if necessary)*/
  62.       regs.x.dx = port;
  63.       int86( 0x14, ®s, ®s);
  64.       return regs.h.al;
  65. }
  66.  
  67. void deinit_comm(void)
  68. {
  69.       union REGS regs;
  70.  
  71.       regs.h.ah = 0x05;         /* deinitialize port (pseudo close) */
  72.       regs.h.al = 0x00;         /* (lower DTR) */
  73.       regs.x.dx = port;
  74.       int86( 0x14, ®s, ®s);
  75. }
  76.  
  77. void main(void)
  78. {
  79.       int ch;
  80.  
  81.       init_comm(0xE3);          /* hard coded 0xB3 = 2400,N,8,1 */
  82.       cputs("MTERM ready!   Press F1 to exit.\r\n");
  83.       while (!exiting)
  84.       {
  85.             if (kbhit())        /* key was hit */
  86.             {
  87.                   ch = getch();     /* Regular ASCII keys are returned as the
  88.                                        ASCII code; function keys, arrows, etc.
  89.                                        as zero followed by a special code
  90.                                        (on next getch.)  */
  91.                   if (ch != 0)
  92.                   {
  93.                         send_char((char)ch);    /* to com port */
  94.                         if (local_echo)
  95.                         {
  96.                               putch(ch);        /* to screen */
  97.  
  98.                               /* add LF to CR? */
  99.  
  100.                               if (cr_add_lf && ch == '\r')
  101.                                     putch('\n');
  102.                         }
  103.                   }
  104.                   else
  105.                   {
  106.                         ch = getch();       /* get the special key code */
  107.                         switch (ch)
  108.                         {
  109.                         case 0x3B: /* F1 */
  110.                               exiting = 1;               /* quit now */
  111.                               break;
  112.  
  113.                         case 0x3C: /* F2 */
  114.                               local_echo = !local_echo;  /* toggle echo */
  115.                               break;
  116.  
  117.                         case 0x3D: /* F3 */
  118.                               cr_add_lf = !cr_add_lf;    /* toggle LF */
  119.                               break;
  120.                         }
  121.                   }
  122.             } /* end if kbhit */
  123.  
  124.             if (input_ready())       /* com port */
  125.             {
  126.                   ch = get_char();
  127.                   putch(ch);
  128.                   if (cr_add_lf && ch == '\r')  /* add LF to CR? */
  129.                         putch('\n');
  130.             }
  131.       } /* end while not exiting */
  132.       deinit_comm();
  133. }    
  134.